package core.manager;

import org.apache.log4j.Logger;

import core.descriptors.TaskDescriptor;
import core.factory.TQProxyFactory;
import core.proxy.TQueueManagerProxy;
import core.util.COREServiceRegistry;
import core.worker.Address;

/**
 * La Classe Activity gestisce l'assegnazione dei Task.
 * Al suo interno ha una WorkerNetwork con gli indirizzi dei Worker a cui passare i Task contenuti in un Workflow
 * @author Vincenzo Merlo, Scuderi Giovanni Luca, Paolo Giuffrida
 */
public class Activity {

    private final Logger logger = COREServiceRegistry.getLogger(Activity.class.toString());
    private Workflow wf;
    private WorkerNetwork sbn;
    private TQueueManagerProxy wkProxy; //TODO: controllare istanze P2P
    private TQProxyFactory tqProxy;

    /**
     *
     */
    public Activity() {
        logger.trace(" costructor");
        wf = null;
        sbn = null;
        wkProxy = null;
    }

    //TODO: Rendere più intelligente  magari con l'uso di thread
    /**
     * Ogni Task del Workflow viene assegnato ad un Worker presente nella WorkerNetwork
     */
    public void scheduleActivity() {
        logger.trace(" scheduledActivity");
        this.tqProxy = TQProxyFactory.getInstance();
        this.setWkProxy(this.wkProxy = tqProxy.getProxy());
        TaskDescriptor td;
        Address addr;
        WorkerNetwork wn = this.getSbn();
        logger.trace("ScheduledActivity - getTask TASK SIZE " + wf.getTasks().size());
        for (int i = 0; i < wf.getTasks().size(); i++) {
            td = wf.getTask(i);
            //logger.trace("ScheduledActivity - getTask i: " + i + " istanza :" + td);
            //logger.trace("ScheduledActivity - getWorker i: " + i);
            //logger.trace(" ScheduledActivity - Wn size : " + wn.getWorkers().size());
            logger.debug(" ScheduledActivity - Wn  : " + wn);
            addr = wn.getWorker(i);

            wkProxy.setAddr(addr);
            logger.debug("Address " + addr);
            wkProxy.enqueue(td); 
            //TODO: ritorna l'esito della euqueue ma la funzione � vooid!!!!! quindi??
         //   logger.debug("Enqueue task, " + td + " esito: " +  ret); //TODO: in caso di errore enqueue torna -1
        }

    }

    /**
     * Ritorna il Workflow
     * @return Workflow
     */
    public Workflow getWf() {
        logger.trace(" getWf");
        return wf;
    }

    /**
     * Setta il Workflow
     * @param Workflow da settare
     */
    public void setWf(Workflow wf) {
        logger.trace(" setWf");
        this.wf = wf;
    }

    /**
     * Ritorna la WorkerNetwork
     * @return WorkerNetwork
     */
    public WorkerNetwork getSbn() {
        logger.trace(" getSbn");
        return sbn;
    }

    /**
     * Setta la WorkerNetwork
     * @param WorkerNetwork da settare
     */
    public void setSbn(WorkerNetwork wn) {
        logger.trace(" setSbn");
        this.sbn = wn;
    }

    /**
     * Ritorna il TQueueManagerProxy
     * @return TQueueManagerProxy
     */
    public TQueueManagerProxy getWkProxy() {
        logger.trace(" getWkProxy");
        return wkProxy;
    }

    /**
     * Setta il TQueueManagerProxy
     * @param TQueueManagerProxy da settare
     */
    public void setWkProxy(TQueueManagerProxy wkProxy) {
        logger.trace(" setWkProxy");
        this.wkProxy = wkProxy;
    }

    /**
     *
     * @return string
     */
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder(Activity.class.getSimpleName());
        sb.append("\n\tWorkflow: ").append(this.getWf()).append("\n\tWorkerNetwork: ").append(this.getSbn()).append("\n\tTQueueManagerProxy: ").append(this.getWkProxy());

        return sb.toString();
    }
}
